// document.write("<script type='text/javascript' src='/plugin/jquery/jquery.min.js'></script>");
document.write("<script type='text/javascript' src='/plugin/SheetJs/xlsx.core.min.js'></script>");
/**
 *@JavaScriptFileName: xyh.js
 *@Description: 工具类
 *@Author: 徐一贺
 */
let xyh = {
    /**
     * 前端页面显示Excel数据
     * @param obj
     * @param e
     */
    displayExcel: function (obj, e) {
        try {
            new Uint8Array([1, 2]).slice(0, 2);
        } catch (e) {
            console.log("[Uint8Array" + e.description + "]这里使用【Array.slice】。");
            //IE或有些浏览器不支持Uint8Array.slice()方法。改成使用Array.slice()方法
            Uint8Array.prototype.slice = Array.prototype.slice;
        }
        var url = $(obj).val();
        if (!url) {
            return;
        }
        var suffix = url.substring(url.lastIndexOf(".") + 1, url.length);
        if (!(suffix == "xls" || suffix == "xlsx")) {
            return alert("文件类型不正确");
        }
        var files = e.target.files;
        var fileReader = new FileReader();
        fileReader.readAsArrayBuffer(files[0]);// 以二进制方式打开文件
        fileReader.onload = function (ev) {
            $(obj).val('');
            var date = new Date();
            var divID = "showExcelDatePanel" + date.getSeconds();
            if ($("#" + divID) != undefined) {
                $("#" + divID).remove();
            }
            $('body').append('<div class="modal fade" id="' + divID + '" style="width: 1300px">' +
                '    <div class="modal-dialog">' +
                '        <div class="modal-content">' +
                '            <div class="modal-header">' +
                '                <button type="button" class="close" data-dismiss="modal" aria-label="Close">' +
                '                    <span aria-hidden="true">&times;</span></button>' +
                '                <h4 class="modal-title">上传Excel文件查看</h4>' +
                '            </div>' +
                '            <div class="modal-body"><table id="excelDataTable"></table></div>' +
                '            <div class="modal-footer">' +
                '                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>' +
                '            </div>' +
                '        </div>' +
                '    </div>' +
                '</div>');
            try {
                var data = ev.target.result;
                var workbook = XLSX.read(data, {type: 'array'}),
                    persons = [];
            } catch (e) {
                console.error(e);
                return alert('文件类型不正确!');
            }

            //遍历每张表读取
            for (var sheet in workbook.Sheets) {
                if (workbook.Sheets.hasOwnProperty(sheet)) {
                    persons = persons.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet], {raw: true, header: 1}));
                    console.log(JSON.stringify(persons));
                    for (var i = 0; i < persons.length; i++) {
                        var person = persons[i];
                        var tr = '<tr id="tr-' + i + '">';
                        for (var j = 0; j < person.length; j++) {
                            tr += '<td>' + $.trim(person[j] ? person[j] : "") + '</td>';
                        }
                        for (var k = 0; k < (6 - person.length); k++) {
                            tr += '<td></td>';
                        }

                        tr += "</td>";
                        $("#excelDataTable").append(tr);
                    }
                    break;// 如果只取第一张表，就把beak注释取消
                }
            }
            $("#" + divID).modal('show');
        };
    },
    /**四舍五入*/
    rounding: function (val, v2) {
        if (isNaN(val) || val == undefined || val == null) {
            return null;
        }
        return Math.round(val * v2) / v2;
    },
    /**删除数组中的指定项*/
    arrayRemoveItem: function (arr, delVal) {
        if (arr instanceof Array) {
            var index = arr.indexOf(delVal);
            if (index > -1) {
                arr.splice(index, 1);
            }
        }
    },
    /**
     * 转换中文数字
     * @param num
     * @returns {function(*=): string|string}
     */
    toChinesNumFun: function (num) {
        let changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']; //changeNum[0] = "零"
        let unit = ["", "十", "百", "千", "万"];
        num = parseInt(num);
        let getWan = (temp) => {
            let strArr = temp.toString().split("").reverse();
            let newNum = "";
            for (let i = 0; i < strArr.length; i++) {
                newNum = (i == 0 && strArr[i] == 0 ? "" : (i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? "" : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i]))) + newNum;
            }
            return newNum;
        }
        let overWan = Math.floor(num / 10000);
        let noWan = num % 10000;
        if (noWan.toString().length < 4) noWan = "0" + noWan;
        return overWan ? getWan(overWan) + "万" + getWan(noWan) : getWan(num);
    },
    //阻止鼠标右键
    disableRight: function () {
        document.oncontextmenu = () => false;
    },
    //阻止选中
    disableSelectstart: function () {
        document.onselectstart = () => false;
    },
    //禁止调试
    disableDebug: function () {
        //禁用右键（防止右键查看源代码）
        document.oncontextmenu = () => false;
        //禁止任何键盘敲击事件（防止F12和shift+ctrl+i调起开发者工具）
        window.onkeydown = window.onkeyup = window.onkeypress = function () {
            window.event.returnValue = false;
            return false;
        }
        //如果用户在工具栏调起开发者工具，那么判断浏览器的可视高度和可视宽度是否有改变，如有改变则关闭本页面
        var h = window.innerHeight, w = window.innerWidth;
        window.onresize = function () {
            if (h != window.innerHeight || w != window.innerWidth) {
                window.close();
                window.location = "about:blank";
            }
        }
    },
    //获取操作系统
    getOS: function () {
        var sUserAgent = navigator.userAgent;
        var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");
        var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh") || (navigator.platform == "MacIntel");
        if (isMac) return "Mac";
        var isUnix = (navigator.platform == "X11") && !isWin && !isMac;
        if (isUnix) return "Unix";
        var isLinux = (String(navigator.platform).indexOf("Linux") > -1 && String(navigator.platform).indexOf("Android") == -1);
        if (isLinux) return "Linux或Android";
        if (isWin) {
            var isWin2K = sUserAgent.indexOf("Windows NT 5.0") > -1 || sUserAgent.indexOf("Windows 2000") > -1;
            if (isWin2K) return "Windows 2000";
            var isWinXP = sUserAgent.indexOf("Windows NT 5.1") > -1 || sUserAgent.indexOf("Windows XP") > -1;
            if (isWinXP) return "Windows XP";
            var isWin2003 = sUserAgent.indexOf("Windows NT 5.2") > -1 || sUserAgent.indexOf("Windows 2003") > -1;
            if (isWin2003) return "Windows 2003";
            var isWinVista = sUserAgent.indexOf("Windows NT 6.0") > -1 || sUserAgent.indexOf("Windows Vista") > -1;
            if (isWinVista) return "Windows Vista";
            var isWin7 = sUserAgent.indexOf("Windows NT 6.1") > -1 || sUserAgent.indexOf("Windows 7") > -1;
            if (isWin7) return "Windows 7";
            var isWin10 = sUserAgent.indexOf("Windows NT 10") > -1 || sUserAgent.indexOf("Windows 10") > -1;
            if (isWin10) return "Windows 10";
        }
        return navigator.platform;
    },
    //获取浏览器
    getBrowser: function () {
        let is360 = false;
        let isIE = false;
        let isFirefox = false;
        let isChrome = false;
        let isEdge = false;
        let broName = 'Runing';
        let str = '';
        let strStart = 0;
        let strStop = 0;
        let arr = new Array();
        let temp = '';
        let userAgent = window.navigator.userAgent; //包含以下属性中所有或一部分的字符串：appCodeName,appName,appVersion,language,platform
        /*alert(userAgent);*/
        //FireFox
        if (userAgent.indexOf('Firefox') != -1) {
            isFireFox = true;
            /*broName = 'FireFox浏览器';*/
            strStart = userAgent.indexOf('Firefox');
            temp = userAgent.substring(strStart);
            broName = "浏览器:"+temp.replace('/', '<br>版本号：')

        }
        //Edge
        if (userAgent.indexOf('Edge') != -1) {
            isEdge = true;
            /*broName = 'Edge浏览器';*/
            strStart = userAgent.indexOf('Edge');
            temp = userAgent.substring(strStart);
            broName = "浏览器："+temp.replace('/', '<br>版本号：');
        }
        //IE浏览器
        if (userAgent.indexOf('NET') != -1 && userAgent.indexOf("rv") != -1) {
            isIE = true;
            /*broName = 'IE浏览器'; */
            strStart = userAgent.indexOf('rv');
            strStop = userAgent.indexOf(')');
            temp = userAgent.substring(strStart, strStop);
            broName = "浏览器："+temp.replace('rv', 'IE').replace(':', '，<br>版本号：');
        }
        //360极速模式可以区分360安全浏览器和360极速浏览器
        if (userAgent.indexOf('WOW') != -1 && userAgent.indexOf("NET") < 0 && userAgent.indexOf("Firefox") < 0) {
            if (navigator.javaEnabled()) {
                is360 = true;
                broName = "浏览器："+'360安全浏览器-极速模式<br>';
            } else {
                is360 = true;
                broName = "浏览器："+'360极速浏览器-极速模式<br>';
            }
        }
        //360兼容
        if (userAgent.indexOf('WOW') != -1 && userAgent.indexOf("NET") != -1 && userAgent.indexOf("MSIE") != -1 && userAgent.indexOf("rv") < 0) {
            is360 = true;
            broName = "浏览器："+'360兼容模式<br>';
        }
        //Chrome浏览器 PC
        if (userAgent.indexOf('WOW') < 0 && userAgent.indexOf("Edge") < 0 && userAgent.indexOf("Mobile") < 0) {
            isChrome = true;
            /*broName = 'Chrome浏览器';*/
            strStart = userAgent.indexOf('Chrome');
            strStop = userAgent.indexOf(' Safari');
            temp = userAgent.substring(strStart, strStop);
            broName = "浏览器："+"PC端" + temp.replace('/', '<br>版本号：');
        }
        //Chrome浏览器 移动端
        if (userAgent.indexOf('WOW') < 0 && userAgent.indexOf("Edge") < 0 && userAgent.indexOf("Mobile") >= 0) {
            isChrome = true;
            /*broName = 'Chrome浏览器';*/
            strStart = userAgent.indexOf('Chrome');
            strStop = userAgent.indexOf(' Safari');
            temp = userAgent.substring(strStart, strStop);
            broName = "浏览器："+"移动端 " + temp.replace('/', '<br>版本号：');
        }
        return broName;
    },
    /**
     *@FunctionName: inputDecimal
     *@Description: 只能输入数字和小数，最多输入两位小数
     *@Author: 徐一贺
     *@CreateDate: 2020/4/2 8:50
     *@Phone: 18210416791
     *@Param: dom
     *@Version: 1.0.0
     */
    inputDecimalTwo:function (obj) {
        obj.value = obj.value.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符
        obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
        obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
        obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');//只能输入两个小数
        if (obj.value.indexOf(".") < 0 && obj.value != "") {//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
            obj.value = obj.value;
        }
    },
    /**
     *@FunctionName: inputDecimal
     *@Description: 只能输入数字和小数，最多输入两位小数
     *@Author: 徐一贺
     *@CreateDate: 2020/4/2 8:50
     *@Phone: 18210416791
     *@Param: dom
     *@Version: 1.0.0
     */
    inputDecimalThree:function (obj) {
        obj.value = obj.value.replace(/[^-\d.]/g, "");  //清除“数字”和“.”以外的字符
        obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
        obj.value = obj.value.replace(/\-{2,}-/g, "-"); //只保留第一个. 清除多余的
        obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
        obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d\d).*$/, '$1$2.$3');//只能输入两个小数
        if (obj.value.indexOf(".") < 0 && obj.value != "") {//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
            obj.value = obj.value;
        }
    },
    inputDecimalThreeHaveNegative:function (obj) {
        var fa="";
        if(obj.value.substring(0,1)=="-")
            fa="-";
        var str=(obj.value.replace(/[^0-9.]/g,'')).replace(/[.][0-9]*[.]/, '.');
        if (str.substring(0,1)==".")
            str="0"+str;
        obj.value=fa+str;
        obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d\d).*$/, '$1$2.$3');//只能输入两个小数
        if (obj.value.indexOf(".") < 0 && obj.value != "") {//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
            obj.value = obj.value;
        }
    }


};